//
//  States_and_CapitalsViewController.m
//  States and Capitals
//
//  Created by Ward Fred on 10/25/10.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "States_and_CapitalsViewController.h"
#import "MapView.h"//;
#import "StartView.h"
#import "ScrollViewDelegate.h"//;
#import "StartViewController.h"//;
@implementation States_and_CapitalsViewController
@synthesize mapView, scrollView;
@synthesize states, isCapitals, isPostal, timerIsValid;
@synthesize backButton;
@synthesize time;
@synthesize myTimer;


/*
 // The designated initializer. Override to perform setup that is required before the view is loaded.
 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 // Custom initialization
 }
 return self;
 }
 */


//Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView { //load the view (this is called whenever s_a_c view controller.view is referenced when view == nil
	NSLog(@"view loading....");
	self.wantsFullScreenLayout = YES;
	TouchScrollView *view = [[TouchScrollView alloc] initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].applicationFrame.size.height) chosenStates:states];
	//self.navigationController.view.frame = CGRectMake(0,0,[UIScreen mainScreen].applicationFrame.size.width, [UIScreen mainScreen].applicationFrame.size.height);
	self.view = view;
	self.scrollView = view;
	//[self.navigationController setNavigationBarHidden:YES];'
	NSLog(@"origin of view: %@", NSStringFromCGPoint(scrollView.frame.origin));
	//scrollView.frame = CGRectMake(0, 20, 320, 460);
	NSLog(@"origin of view: %@", NSStringFromCGPoint(scrollView.frame.origin));
	//self.navigationController.view.frame = [UIScreen mainScreen].applicationFrame;
	self.navigationController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
	[view release];
}




// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad { //set up the scroll view after it has loaded -- non-essential stuff that can happen after the view is loaded
	NSLog(@"view did load");
    [super viewDidLoad];
	//scrollView = [[TouchScrollView alloc] initWithFrame:self.view.bounds];
	
	//start the timer
	time = 0.0;
	NSLog(@"%f",time);
	myTimer = [NSTimer 
               scheduledTimerWithTimeInterval:.1
               target: self
               selector: @selector(updateTime:)
               userInfo:nil
               repeats: YES];
	timerIsValid = YES;
	
	
	
	//pass whether capital, postalabbrevs, or both are tested
	scrollView.isCapitals = isCapitals;
	scrollView.isPostal = isPostal;
	
	scrollView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
	scrollView.contentSize = [UIImage imageNamed:@"Colored_US_Map.png"].size;
	
	
	scrollView.maximumZoomScale = 1; // max and min zoom MUST be set
	scrollView.minimumZoomScale = ([[UIScreen mainScreen] bounds].size.height-20)/[UIImage imageNamed:@"Colored_US_Map.png"].size.height;//so we can't zoom out too far
	
	ScrollViewDelegate *d = [[ScrollViewDelegate alloc] init];
	scrollView.delegate = d;
	backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 15, 15)];
	[backButton setBackgroundImage:[UIImage imageNamed:@"buttonX.gif"] forState:UIControlStateNormal];
	[backButton addTarget:self action: @selector(moveBackward) forControlEvents:UIControlEventTouchUpInside];
	[scrollView addSubview:backButton];
	scrollView.backButton = backButton;
	[scrollView setZoomScale:scrollView.minimumZoomScale animated:NO]; //scroll out to start
    [backButton release];

}
-(void)moveBackward{
	NSLog(@"BackButton");
    
	if(timerIsValid)
        [myTimer invalidate];
	
	//NSLog(@"clicked");
	NSLog(@"Timer not at issue");
	[self.navigationController popViewControllerAnimated:NO];
	//NSLog(@"clacked");
}

-(void)updateTime:(NSTimer *)timer{
	
	if([scrollView.statesForQuiz count] ==0){//if they done
		
		//prints to 1 decimal place
        scrollView.quizLabel.text = [NSString stringWithFormat:@"%.1f seconds", time];
		timerIsValid = NO;
		[myTimer invalidate];
		
		
	}
	else{
		time+=.1;	
		//NSLog(@"%f",time);
        
	}
}

-(void)viewWillAppear:(BOOL)animated{
	[super	viewWillAppear:animated];
	NSLog(@"view appearing");
}
-(void)viewWillDisappear:(BOOL)animated{
	[super viewWillDisappear:animated];
	NSLog(@"view disappearing");
}
-(void)viewDidUnload{
	[super viewDidUnload];
}





// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
   /* if([UIScreen mainScreen].applicationFrame.size.height >420){
        scrollView.quizLabel.hidden = NO;
        return YES;
    }*/
	if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)//portrait
	{
        scrollView.quizLabel.frame = CGRectMake(scrollView.contentOffset.x, [UIScreen mainScreen].applicationFrame.size.height - scrollView.quizLabel.bounds.size.height+scrollView.contentOffset.y, scrollView.quizLabel.bounds.size.width, scrollView.quizLabel.bounds.size.height);
		NSLog(@"portrait quizlabel %@", NSStringFromCGRect(scrollView.quizLabel.frame));
	}
	else{//landscape
		scrollView.quizLabel.frame = CGRectMake(scrollView.contentOffset.x, [UIScreen mainScreen].applicationFrame.size.width - scrollView.quizLabel.bounds.size.height+scrollView.contentOffset.y, scrollView.quizLabel.bounds.size.width, scrollView.quizLabel.bounds.size.height);
	}
    
	
	[self.scrollView setNeedsLayout];
	[self.backButton setNeedsLayout];
	[self.navigationController.view setNeedsLayout];
	[self.scrollView setNeedsDisplay];
	[self.backButton setNeedsDisplay];
	[self.navigationController.view setNeedsDisplay];
    return YES;//(interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    scrollView.quizLabel.hidden = YES; //hide label to make rotation cleaner
    
    
    
}





//move lebels when it rotates
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	
	if(fromInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || fromInterfaceOrientation == UIInterfaceOrientationLandscapeRight)//portrait
	{

        scrollView.quizLabel.frame = CGRectMake(scrollView.contentOffset.x, [UIScreen mainScreen].applicationFrame.size.height - scrollView.quizLabel.bounds.size.height+scrollView.contentOffset.y, scrollView.quizLabel.bounds.size.width, scrollView.quizLabel.bounds.size.height);
        
        
        
		
		
	}
	else{//landscape
        
        //label
        scrollView.quizLabel.frame = CGRectMake(scrollView.contentOffset.x, [UIScreen mainScreen].applicationFrame.size.width - scrollView.quizLabel.bounds.size.height+scrollView.contentOffset.y, scrollView.quizLabel.bounds.size.width, scrollView.quizLabel.bounds.size.height);
        
        
	}

	scrollView.quizLabel.hidden = NO;
	[self.scrollView setNeedsLayout];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
	NSLog(@"view receiving touch: %@", NSStringFromClass([[[touches anyObject] view] class]));
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	// Release any cached data, images, etc that aren't in use.
}




- (void)dealloc {
    [super dealloc];
}

@end
